<?php

function __autoload($class_name) {
    include 'class.' . strtolower($class_name) . '.php';
}

class CutiDao {

    public function setujuiCuti($id,$stat){
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "UPDATE t_cuti SET ";
        if($stat=="ganti"){
            $query.=" idGanti=1,idProses=2 ";
        }
        if($stat=="atasan"){
            $query.=" idAju=1,idProses=3  ";
        }
        if($stat=="setuju"){
            $query.=" idSetuju=1,idProses=4  ";
        }

        $query.=" WHERE id='$id'";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }

    public function getAllCutiGanti($tahun, $nip) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);
        $query = "SELECT * FROM t_cuti WHERE tahun='$tahun' AND nipGanti='$nip' AND idProses=1";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }
    public function getAllCutiAtasan($tahun, $nip) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);
        $query = "SELECT * FROM t_cuti WHERE tahun='$tahun' AND nipAtasan='$nip' AND idProses=2";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }
    public function getAllCutiSetuju($tahun, $nip) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);
        $query = "SELECT * FROM t_cuti WHERE tahun='$tahun' AND nipSetuju='$nip' AND idProses=3";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }

    public function getAllCutiNow($nip,$tglSkrg) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);
        $query = "SELECT * FROM t_cuti WHERE nip='$nip' AND idProses=4 AND flag=1 AND ('$tglSkrg' BETWEEN tglAwal AND tglAkhir)";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return $result->fetch();
        }

        return false;
    }



    public function cekCuti($nip, $tglAwal, $tglAkhir) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "SELECT * FROM t_cuti WHERE nip='$nip' ";
        $query .=" AND (tglAwal BETWEEN '$tglAwal' AND '$tglAkhir' ";
        $query .= " OR  tglAkhir BETWEEN '$tglAwal' AND '$tglAkhir') ";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return true;
        }

        return false;
    }

    public function getCuti($id) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "SELECT * FROM t_cuti WHERE id='$id'";
        //print_r($query);
        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            $data = $result->fetch();
            return $data;
        }

        return false;
    }

    public function getAllCuti($tahun, $nip,$status) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);
        $query = "SELECT * FROM t_cuti WHERE tahun='$tahun' AND nip='$nip' AND idProses='$status'";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }

    public function getAllCutiBatal($tahun, $nip) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);
        $query = "SELECT * FROM t_cuti WHERE tahun='$tahun' AND nip='$nip' AND (idProses=5 OR idProses=6)";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }

    public function getAllCutiToBatal($tahun, $nip) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $now=date('Y-m-d');

        $query = "SELECT * FROM t_cuti WHERE tahun='$tahun' AND nip='$nip' AND idProses<>1";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }

    public function getAllCutiToKartu($tahun, $nip) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "SELECT * FROM t_cuti WHERE tahun='$tahun' AND nip='$nip' AND flag=0 AND idGanti=1 AND idAju=1 AND idSetuju=1 AND (idProses=4 OR idProses=5 OR idProses=6)";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }

     public function getAllCutiSudahKartu($tahun, $nip) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "SELECT * FROM t_cuti WHERE tahun='$tahun' AND nip='$nip' AND flag=1 AND idGanti=1 AND idAju=1 AND idSetuju=1 AND (idProses=4 OR idProses=5 OR idProses=6)";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }

    public function getAllCutiUsul($tahun, $nip) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);
        $query = "SELECT * FROM t_cuti WHERE tahun='$tahun' AND nip='$nip' AND idProses<4";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }
    public function getAllCutiTgl($tglAwal, $tglAkhir) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);
        $query = "SELECT * FROM t_cuti WHERE tglAwal>='$tglAwal' AND tglAkhir<='$tglAkhir'";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }

    public function getAllCutiCetak($tahun, $nip) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $skrg=date('Y-m-d');
        $query = "SELECT * FROM t_cuti WHERE tahun='$tahun' AND nip='$nip' AND tglAwal>'$skrg'";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }
    
    public function getCutiSedang($tahun, $nip,$tgl) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);
        $query = "SELECT * FROM t_cuti WHERE tahun='$tahun' AND nip='$nip' AND idProses='4' AND  ('$tgl' BETWEEN tglAwal AND tglAkhir)";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() >= 1) {
            return $result->fetchAll();
        }

        return false;
    }

    public function createCuti(Cuti $cuti) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);


        $query = "INSERT INTO t_cuti(nip,tahun,tglAwal,tglAkhir,idJnsCuti,nipAtasan,nipGanti,nipSetuju,jmlHari,alamat,telp,tglAju,idProses) ";
        $query.=" VALUES('$cuti->nip','$cuti->tahun','$cuti->tglAwal','$cuti->tglAkhir','$cuti->idJnsCuti',";
        $query.="'$cuti->nipAtasan','$cuti->nipGanti','$cuti->nipSetuju','$cuti->jmlHari','$cuti->alamat',";
        $query.="'$cuti->telp','$cuti->tglAju','$cuti->idProses')";

        //print_r($query);
        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }

    public function deleteCuti($id) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "DELETE FROM t_cuti WHERE id='$id' AND flag=0";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }

    public function updateCuti(Cuti $cuti) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "UPDATE t_cuti SET tglAwal='$cuti->tglAwal',tglAkhir='$cuti->tglAkhir',tahun='$cuti->tahun',idJnsCuti='$cuti->idJnsCuti', ";
        $query.=" nipAtasan='$cuti->nipAtasan',nipGanti='$cuti->nipGanti',nipSetuju='$cuti->nipSetuju',jmlHari='$cuti->jmlHari',alamat='$cuti->alamat',";
        $query.=" telp='$cuti->telp' ";
        $query.=" WHERE id='$cuti->id'";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }

    public function batalCuti($id,$idProses) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "UPDATE t_cuti SET idProses='$idProses' WHERE id='$id'";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }

    public function KartukanCuti($id) {
        $db = Database::getInstance();
        $con = $db->getConnection(1);

        $query = "UPDATE t_cuti SET flag=1 WHERE id='$id'";

        $result = $con->prepare($query);
        $result->execute();

        if ($result->rowCount() == 1) {
            return true;
        }

        return false;
    }

}

class Cuti {

    var $id;
    var $nip;
    var $tglAju;
    var $tglMulai;
    var $tglAkhir;
    var $tahun;
    var $idJnsCuti;
    var $nipGanti;
    var $nipAtasan;
    var $nipSetuju;
    var $jmlHari;
    var $alamat;
    var $telp;
    var $idGanti;
    var $idAju;
    var $idSetuju;
    var $idProses;
    var $flag;

}
